package se.hiq.wineo.server;

import se.hiq.winedb.WineDB;
import se.hiq.winedb.datamodel.Wine;
import se.hiq.wineo.client.WineService;
import se.hiq.wineo.shared.data.WineColor;
import se.hiq.wineo.shared.data.WineDescriptor;
import se.hiq.wineo.shared.data.WineDetails;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class WineServiceImpl extends RemoteServiceServlet implements WineService {

	@Override
	public WineDescriptor[] getAvailableWines() {
		return WineTools.toWineDescriptors(WineDB.getAvailableWines());
	}
	
	@Override
	public WineDetails getWineDetails(Long primaryKey) {
		Wine w = WineDB.getWine(primaryKey);
		if (w != null) {
			return WineTools.toWineDetails(w);
		} else {
			return null;
		}
	}

	@Override
	public WineDetails updateOrStoreWine(WineDetails wine) {
		return WineTools.toWineDetails(WineDB.updateOrStoreWine(WineTools.toWine(wine)));
	}

	@Override
	public WineDescriptor[] getWines(String name, Integer year, WineColor color) {
		return WineTools.toWineDescriptors(WineDB.getWines(name, year, WineTools.toServerColor(color)));
	}
}
